package com.unitd.base.module.master.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unitd.base.module.master.mybatis.mapper.DatasourceBaseMapper;
import com.unitd.base.module.master.pojo.dto.DatasourceBaseVO;
import com.unitd.base.module.master.pojo.po.DatasourceBasePO;
import com.unitd.base.module.master.service.DatasourceBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author Datasource
 * @version 1.0
 * @filename DatasourceServiceImpl.java
 * @copyright www.pactera.com
 * @date 2020-08-28
 */
@Slf4j
@Service
public class DatasourceBaseBaseServiceImpl extends ServiceImpl<DatasourceBaseMapper, DatasourceBasePO> implements DatasourceBaseService {

    /**
     * 登录用户后去需要绑定的数据源列表
     *
     * @return 需要绑定的数据源列表
     */
    @Override
    public DatasourceBaseVO getById(Long dbId) {
        if (dbId == null || dbId == 0) {
            return null;
        }

        // 获取当前登录用户的默认数据源
        DatasourceBasePO datasource = baseMapper.selectById(dbId);
        return convertPoToVo(datasource);
    }

    /**
     * 将po对象转换成vo对象
     *
     * @param po 数据库实体对象
     * @return 数据交互、展示对象
     */
    private DatasourceBaseVO convertPoToVo(DatasourceBasePO po) {

        DatasourceBaseVO vo = new DatasourceBaseVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }
}